package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.IDictTypeService;
import com.ibeeking.found.common.base.BaseDO;
import com.ibeeking.gmc.global.service.common.bo.DictTypeBO;
import com.ibeeking.gmc.global.service.common.constants.RedisKeyConstant;
import com.ibeeking.gmc.global.service.common.dos.DictTypeDO;
import com.ibeeking.gmc.global.service.common.param.DictTypeParam;
import com.ibeeking.gmc.global.service.common.query.DictTypePageQuery;
import com.ibeeking.gmc.global.service.common.vo.DictTypePageVO;
import com.ibeeking.gmc.global.service.common.vo.DictTypeVO;
import com.ibeeking.gmc.global.service.mapper.DictTypeMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.redis.annotation.CacheEvict;
import com.ibeeking.nematos.redis.annotation.CacheQuery;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @ClassName DictTypeServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2020-12-02 16:06
 **/
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictTypeDO> implements IDictTypeService {

    @Resource
    private DictTypeMapper dictTypeMapper;

    @Override
    public Page<DictTypePageVO> pageList(DictTypePageQuery dictTypePageQuery) {
        LambdaQueryWrapper<DictTypeDO> queryWrapper = new LambdaQueryWrapper<>(DictTypeDO.class);
        queryWrapper.orderByDesc(BaseDO::getCreateTime);

        if (null != dictTypePageQuery.getPublishStatus()) {
            queryWrapper.eq(DictTypeDO::getPublishStatus, dictTypePageQuery.getPublishStatus());
        }

        if (StringUtils.isNotBlank(dictTypePageQuery.getType())) {
            queryWrapper.like(DictTypeDO::getType, dictTypePageQuery.getType());
        }

        if (StringUtils.isNotBlank(dictTypePageQuery.getName())) {
            queryWrapper.like(DictTypeDO::getName, dictTypePageQuery.getName());
        }
        Page<DictTypeDO> page = dictTypeMapper.selectPage(new Page<>(dictTypePageQuery.getPageNum(), dictTypePageQuery.getPageSize()), queryWrapper);
        return BeanUtil.convertPage(page, DictTypePageVO.class);
    }

    @Override
    public Boolean add(DictTypeParam dictTypeParam) {
        IDictTypeService bean = SpringBeanUtil.getBean(IDictTypeService.class);
        if (bean.checkType(dictTypeParam.getType())) {
            throw new BusinessException("存在同名字典类型");
        }
        DictTypeDO dictTypeDO = BeanUtil.convertBean(dictTypeParam, DictTypeDO.class);
        dictTypeDO.setCreateTime(LocalDateTime.now());
        dictTypeDO.setModifyTime(LocalDateTime.now());
        return dictTypeMapper.insert(dictTypeDO) > 0;
    }

    @CacheEvict(cacheName = RedisKeyConstant.MODULE + RedisKeyConstant.GLOBAL_DICT_TYPE, key = "#dictTypeParam.id")
    @Override
    public Boolean modify(DictTypeParam dictTypeParam) {
        IDictTypeService bean = SpringBeanUtil.getBean(IDictTypeService.class);
        DictTypeDO dictTypeDO = BeanUtil.convertBean(dictTypeParam, DictTypeDO.class);
        dictTypeDO.setType(null);
        dictTypeDO.setModifyTime(LocalDateTime.now());
        return dictTypeMapper.updateById(dictTypeDO) > 0;
    }

    @Override
    public DictTypeVO view(Long id) {
        IDictTypeService bean = SpringBeanUtil.getBean(IDictTypeService.class);
        DictTypeBO dictTypeBO = bean.queryById(id);
        if (null == dictTypeBO) {
            return null;
        }
        return BeanUtil.convertBean(dictTypeBO, DictTypeVO.class);
    }

    @CacheQuery(cacheName = RedisKeyConstant.MODULE + RedisKeyConstant.GLOBAL_DICT_TYPE, key = "#id", expires = RedisKeyConstant.MONTH)
    @Override
    public DictTypeBO queryById(Long id) {
        DictTypeDO dictTypeDO = dictTypeMapper.selectById(id);
        if (null == dictTypeDO) {
            return null;
        }
        return BeanUtil.convertBean(dictTypeDO, DictTypeBO.class);
    }

    @CacheEvict(cacheName = RedisKeyConstant.MODULE + RedisKeyConstant.GLOBAL_DICT_TYPE, key = "#id")
    @Override
    public Boolean changeStatus(Long id, Integer status, Long userId) {
        DictTypeDO dictTypeDO = new DictTypeDO();
        dictTypeDO.setId(id);
        dictTypeDO.setPublishStatus(status);
        dictTypeDO.setModifyBy(userId);
        dictTypeDO.setModifyTime(LocalDateTime.now());
        return dictTypeMapper.updateById(dictTypeDO) > 0;
    }

    @CacheEvict(cacheName = RedisKeyConstant.MODULE + RedisKeyConstant.GLOBAL_DICT_TYPE, key = "#id")
    @Override
    public Boolean del(Long id) {
        return dictTypeMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean checkType(String type) {
        return dictTypeMapper.selectCount(new LambdaQueryWrapper<>(DictTypeDO.class).eq(DictTypeDO::getType, type)) > 0;
    }
}